home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_053 / uuencode / uuencode.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  140 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)uuencode.c    5.3-1 (Berkeley) 1/22/85";
  3. #endif
  4. /* modified by ajr to include checksums */
  5.  
  6. /*
  7.  * uuencode [input] output
  8.  *
  9.  * Encode a file so it can be mailed to a remote system.
  10.  */
  11. #include <stdio.h>
  12.  
  13. #ifdef unix
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #endif
  17.  
  18. #define SUMSIZE 64
  19.  
  20. /* ENC is the basic 1 character encoding function to make a char printing */
  21. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  22.  
  23. main(argc, argv)
  24. char **argv;
  25. {
  26.     FILE *in;
  27. #ifdef unix
  28.     struct stat sbuf;
  29. #endif
  30.     int mode;
  31.  
  32.     /* optional 1st argument */
  33.     if (argc > 2) {
  34.         if ((in = fopen(argv[1], "r")) == NULL) {
  35.             perror(argv[1]);
  36.             exit(1);
  37.         }
  38.         argv++; argc--;
  39.     } else
  40.         in = stdin;
  41.  
  42.     if (argc != 2) {
  43.         printf("Usage: uuencode [infile] remotefile\n");
  44.         exit(2);
  45.     }
  46.  
  47. #ifdef unix
  48.     /* figure out the input file mode */
  49.     fstat(fileno(in), &sbuf);
  50.     mode = sbuf.st_mode & 0777;
  51. #else
  52.     mode = 0777;
  53. #endif
  54.     printf("begin %o %s\n", mode, argv[1]);
  55.  
  56.     encode(in, stdout);
  57.  
  58.     printf("end\n");
  59.     exit(0);
  60. }
  61.  
  62. /*
  63.  * copy from in to out, encoding as you go along.
  64.  */
  65. encode(in, out)
  66. FILE *in;
  67. FILE *out;
  68. {
  69.     char buf[80];
  70.     int i, n, checksum;
  71.  
  72.     for (;;) {
  73.         /* 1 (up to) 45 character line */
  74.         n = fr(in, buf, 45);
  75.         putc(ENC(n), out);
  76.  
  77.         checksum = 0;
  78.         for (i=0; i<n; i += 3)
  79.             checksum = (checksum+outdec(&buf[i], out)) % SUMSIZE;
  80.  
  81.         putc(ENC(checksum), out);
  82.         putc('\n', out);
  83.         if (n <= 0)
  84.             break;
  85.     }
  86. }
  87.  
  88. /*
  89.  * output one group of 3 bytes, pointed at by p, on file f.
  90.  * return the checksum increment.
  91.  */
  92. int outdec(p, f)
  93. char *p;
  94. FILE *f;
  95. {
  96.     int c1, c2, c3, c4;
  97.  
  98.     c1 = *p >> 2;
  99.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  100.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  101.     c4 = p[2] & 077;
  102.     putc(ENC(c1), f);
  103.     putc(ENC(c2), f);
  104.     putc(ENC(c3), f);
  105.     putc(ENC(c4), f);
  106.  
  107.     return((p[0]+p[1]+p[2]) % SUMSIZE);
  108. }
  109.  
  110. /* fr: like read but stdio */
  111. int
  112. fr(fd, buf, cnt)
  113. FILE *fd;
  114. char *buf;
  115. int cnt;
  116. {
  117.     int c, i;
  118.  
  119.     for (i=0; i<cnt; i++) {
  120.         c = getc(fd);
  121.         if (c == EOF)
  122.             return(i);
  123.         buf[i] = c;
  124.     }
  125.     return (cnt);
  126. }
  127.  
  128. #if (!unix && MANX)
  129.  
  130. perror (sp)
  131. char *sp;
  132. {
  133.     if (sp && *sp) {
  134.         fprintf (stderr, "%s: ");
  135.     }
  136.     fprintf (stderr, "<unknown error>\n");
  137. }
  138.  
  139. #endif    /* Unix */
  140.